home *** CD-ROM | disk | FTP | other *** search
/ A Teacher's Guide to the Holocaust / A Teacher's Guide to the Holocaust.iso / data / people / scripts / elemsldrclass.js < prev    next >
Text File  |  1999-12-05  |  7KB  |  216 lines

  1. // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //Constructs a slider element
  4. function MM_sldr(theParent, theName, theInitVal,theMin,theMax,theUseFloat,theIsVertical) {
  5.   // properties
  6.   this.initialValue = Math.min(Math.max(Math.min(theMin,theMax),theInitVal),
  7.                       Math.max(theMax,theMin)); //keep in legal range, may be reversed
  8.   this.value = '';
  9.   this.disabled = true;
  10.  
  11.   this.min = theMin;
  12.   this.max = theMax;
  13.   this.useFloat = theUseFloat;
  14.  
  15.   this._parent = theParent;
  16.   this._name = theName;
  17.   this._obj = '';
  18.   this._isVertical = theIsVertical;
  19.   this._trkObj = '';
  20.   this._txtObj = '';
  21.   this._trackLen = 0;
  22.   this._trackPos = 0;
  23.   this._self = theParent._self+".e['"+theName+"']";
  24.   this.c = new Array();
  25.  
  26.   // member functions
  27.   this.reset = MM_sldrReset;
  28.   this.init = MM_sldrInit;
  29.   this.enable = MM_sldrEnable;
  30.   this.disable = MM_sldrDisable;
  31.   this.setDisabled = MM_sldrSetDisabled;
  32.   this.update = MM_sldrUpdate;
  33.   this.setValue = MM_sldrSetValue;
  34. }
  35.  
  36. //Resets the element
  37. function MM_sldrReset() {
  38.   var i, NS = (navigator.appName == "Netscape");
  39.   if (this._obj) with (this) {
  40.     if (_parent.disabled) disable();
  41.     else enable();
  42.     value = initialValue;
  43.     for (i in c) c[i].validValue();
  44.     if (_txtObj) _txtObj.value = initialValue; //load text object if there
  45.     setValue(initialValue);
  46.   }
  47. }
  48.  
  49. //Enables the element
  50. function MM_sldrEnable() {
  51.   if (this._obj) with (this) {
  52.     disabled = false;
  53.     _obj.MM_dragOk = true;
  54.   }
  55. }
  56.  
  57. //Disables the element
  58. function MM_sldrDisable() {
  59.   this.disabled = true;
  60.   this._obj.MM_dragOk = null;
  61. }
  62.  
  63. //Calls the approppriate disable or enable function
  64. function MM_sldrSetDisabled(theDisabled) {
  65.   if (theDisabled) this.disable();
  66.   else this.enable();
  67. }
  68.  
  69. //Called when the layer is dragged or dropped, updates all values.
  70. function MM_sldrUpdate(dragging) {
  71.   var i;
  72.   if (!this.disabled) with (this) {
  73.     if (_isVertical) value = (min+((max-min)*(_trackLen-_obj.MM_UPDOWN-_trackPos)/_trackLen));
  74.     else value = (min+(max-min)*(1-(_trackLen-_obj.MM_LEFTRIGHT-_trackPos)/_trackLen));
  75.     if (!useFloat) value = Math.round(value);
  76.     if (dragging) {
  77.       if (_txtObj) _txtObj.value = value;
  78.       if (this.onDrag != null) onDrag(_parent._self+_name, value);
  79.     } else {
  80.       for (i in c) c[i].validValue(); // walk choices, setting selected
  81.       if (this.onDrop != null) onDrop(_parent._self+_name, value);
  82.       _parent.update(); // call the parent's update
  83.   } }
  84. }
  85.  
  86. //Initializes slider objects. Looks for drag thumb, track image, and optional textbox.
  87. //Makes the layer draggable by calling MM_dragLayer.
  88. function MM_sldrInit() {
  89.   var baseName,thm,diff,U=0,D=0,L=0,R=0,NS = (navigator.appName == "Netscape");
  90.   with (this) {
  91.     baseName = _parent._self+_name; //assemble base name
  92.     _obj = MM_intFindObject(baseName+"Inp"); //find slider layer object
  93.     thm = MM_intFindObject(baseName+"Thm"); //find slider layer object
  94.     if (_obj && thm) {
  95.       _trkObj = MM_intFindObject(baseName+'Trk');  //find trk image object
  96.       if (_trkObj) {
  97.         _txtObj = MM_intFindObject(baseName+'Val');  //find optional output textfield obj
  98.         _trackLen = Math.max(((_isVertical)?
  99.           ((NS)?_trkObj.height-thm.height : _trkObj.height-thm.height):
  100.           ((NS)?_trkObj.width-thm.width  : _trkObj.width-thm.width)),1);
  101.         diff = this.max-this.min;
  102.         _trackPos = Math.round(((this.initialValue - this.min)/((diff)?diff:1))*_trackLen);
  103.         if (_isVertical) _trackPos = _trackLen - _trackPos;
  104.         setValue(this.initialValue);
  105.         if (_isVertical) {U = _trackPos; D = _trackLen-_trackPos;}
  106.         else {L = _trackPos; R = _trackLen-_trackPos;}
  107.         MM_dragLayer(baseName+"Inp",'',0,0,0,0,false,false,U,D,L,R,false,false,0,
  108.                      _self+'.update()',true,_self+'.update(true)');
  109.   } } }
  110. }
  111.  
  112.  
  113. //Moves the drag thumb to the given position.
  114. function MM_sldrSetValue(newValue) {
  115.   var i, newPos, diff, newPos, posObj, NS = (navigator.appName == "Netscape");
  116.   with (this) {
  117.     newValue = Math.min(Math.max(Math.min(min,max),newValue),
  118.                Math.max(max,min)); //keep in legal range, may be reversed
  119.     diff = max-min;
  120.     newPos = Math.round(((newValue - min)/((diff)?diff:1))*_trackLen);
  121.     if (_isVertical) newPos = _trackLen - newPos;
  122.     posObj = (NS)?_obj:_obj.style;
  123.     posObj[_isVertical?(NS?'top':'pixelTop'):(NS?'left':'pixelLeft')] = newPos;
  124.     if (_txtObj) _txtObj.value = newValue; //load text object if there
  125.     value = newValue;
  126.     for (i in c) c[i].validValue(); // walk choices, setting selected
  127.     if (this.onDrag != null) onDrag(_parent._self+_name, value);
  128.     if (this.onDrop != null) onDrop(_parent._self+_name, value);
  129.     _parent.update(true); // update int, but don't judge
  130.   }
  131. }
  132.  
  133. //------------------------------------------------------------------
  134.  
  135. //Constructs a slider range element.
  136. function MM_sldrRnge(theParent, theElement, theName,
  137.                theExpectedValue, theIsCorrect, theScore) {
  138.   // properties
  139.   this.isCorrect = theIsCorrect;
  140.   this.expectedValue = theExpectedValue;
  141.   this.score = theScore;
  142.   this.selected = false;
  143.   this.disabled = false;
  144.  
  145.   this._elem = eval(theParent._self+".e['"+theElement+"']");
  146.   this._isChoice = true;
  147.  
  148.   // method
  149.   this.validValue = MM_sldrRgneValidValue;
  150.   this.setSelected = MM_sldrRgneSetSelected;
  151.   this.setDisabled = MM_sldrRgneSetDisabled;
  152.   this.getExpRangeValue = MM_sldrRgneGetExpRangeValue;
  153. }
  154.  
  155.  
  156. //Checks if the current slider value is within the current range.
  157. function MM_sldrRgneValidValue() {
  158.   var myValue,expVal,colonPos,fromStr,fromNum,toStr,toNum,retVal=false;
  159.   if (!this.disabled) with (this) {
  160.     fromNum = getExpRangeValue(0);
  161.     toNum   = getExpRangeValue(1);
  162.     myValue = _elem.value;
  163.     if (fromNum <= toNum)
  164.       retVal = (fromNum <= myValue && myValue <= toNum);
  165.     else
  166.       retVal = (toNum <= myValue && myValue <= fromNum);
  167.     selected = retVal;
  168.   }
  169.   return retVal
  170. }
  171.  
  172.  
  173. //Averages the range and sets the slider to that average
  174. function MM_sldrRgneSetSelected(theSelected) {
  175.   with (this) {
  176.     if (theSelected) {
  177.       fromNum = getExpRangeValue(0);
  178.       toNum   = getExpRangeValue(1);
  179.       avgNum = fromNum
  180.       if      (fromNum > toNum) avgNum = toNum + (fromNum - toNum)/2;
  181.       else if (fromNum < toNum) avgNum = fromNum + (toNum - fromNum)/2;
  182.       if (!this._elem.useFloat) avgNum = Math.round(avgNum);
  183.       _elem.setValue(avgNum);
  184.     } else {
  185.       selected = false;
  186.       _elem._parent.update(true);
  187.     }
  188.   }
  189. }
  190.  
  191.  
  192. function MM_sldrRgneSetDisabled(theDisabled) {
  193.   with (this) {
  194.     disabled = theDisabled;
  195.     if (disabled) selected = false;
  196.     else validValue();
  197.     _elem._parent.update(true);
  198.   }
  199. }
  200.  
  201.  
  202. //Given colon separated num string, returns one of the numbers.
  203. //Pass 0 for first num, 1 for second: "33:46" => 33  or 46
  204. function MM_sldrRgneGetExpRangeValue(numIndex) {
  205.   var expVal, colonPos, retVal;
  206.  
  207.   expVal = this.expectedValue;
  208.   colonPos = expVal.indexOf(":");
  209.   if (colonPos != -1) { //if colon separated, split strings
  210.     retVal = expVal.substring((numIndex)?colonPos+1:0,(numIndex)?expVal.length:colonPos);
  211.   } else { //else, theres a single number, no range
  212.     retVal = expVal;
  213.   }
  214.   return (this._elem.useFloat)?parseFloat(retVal):parseInt(retVal);
  215. }
  216.